package com.psychologyCommunity.Community.util;

import com.psychologyCommunity.Community.entity.User;
import org.springframework.stereotype.Component;

//相当于一个容器 ，持有用户信息，用于代替session对象的功能，而且能解决并发下冲突的问题
@Component
public class HostHolder {
    //ThreadLocal 底层 相当于是直接拿到当前线程，以线程为key 取map中对应的值
    private ThreadLocal<User> userThreadLocalInfo=new ThreadLocal<User>();

    public void setUserThreadLocalInfo(User user){
        userThreadLocalInfo.set(user);
    }
    public User getUser(){
      return   userThreadLocalInfo.get();
    }
    public void clear(){
        userThreadLocalInfo.remove();
    }

}
